package it.unibo.iss.domotica;

import java.util.List;

import it.unibo.iss.domotica.edDomain.Elettrodomestico;
import android.app.Dialog;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.content.Context;

public class ListEdSuperfluiDialog extends Dialog implements View.OnClickListener {

	public interface IOnClickListener {
		void onPowerOffClick(Elettrodomestico selected);
		void onAbortClick();
	}
	
	private ArrayAdapter<Elettrodomestico> arrayAdapter;
	private ListView listView;
	private Button powerOffButton;
	private Button abortButton;
	private IOnClickListener onClickListener;
	
	public ListEdSuperfluiDialog(Context context) {
		super(context);

		setContentView(R.layout.listedsuperfluilayout);
		setTitle("Select an household appliance to power off");
		
		listView = (ListView) findViewById(R.id.listEdSuperfluiView);
		arrayAdapter = new ArrayAdapter<Elettrodomestico>(context, android.R.layout.simple_list_item_single_choice);
		listView.setAdapter(arrayAdapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		powerOffButton = (Button) findViewById(R.id.powerOffButton);
		powerOffButton.setOnClickListener(this);
		
		abortButton = (Button) findViewById(R.id.abortButton);
		abortButton.setOnClickListener(this);
	}
	
	public void setOnClikListener(IOnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}
	
	public void setListEdSuperflui(List<Elettrodomestico> listEd){
		arrayAdapter.clear();
		
		for(Elettrodomestico ed : listEd) {
			arrayAdapter.add(ed);
		}
		
//		listView.setItemChecked(0, true);
	}

	@Override
	public void onClick(View v) {
		dismiss();
		
		if((Button)v == powerOffButton) {
			Elettrodomestico selectedEd = null;
			int selectedPosition = listView.getCheckedItemPosition();
			
			if(selectedPosition != ListView.INVALID_POSITION) {
				selectedEd = arrayAdapter.getItem(selectedPosition);
			}
			
			onClickListener.onPowerOffClick(selectedEd);
		}
		else if((Button)v == abortButton) {
			onClickListener.onAbortClick();
		}
	}
}
